//
//  SQRegistrationViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/8.
//

#import "SQRegistrationViewController.h"

#import "SQGetDictionariesModel.h"
#import "SQDangeRegistrationModel.h"

#import "PickerSelectView.h"
#import "HRBDateSelectView.h"
#import "LCImageChoose.h"
#import "SQShowImage.h"

@interface SQRegistrationViewController ()<BaseTableVCInterface>

@property (strong, nonatomic) SQGetDictionariesModel *getDictionariesModel;

@property (weak, nonatomic) IBOutlet UITextField *tf_type;//隐患类别
@property (weak, nonatomic) IBOutlet UITextField *tf_level;//隐患等级
@property (weak, nonatomic) IBOutlet UITextField *tf_rectificationType;//隐患整改类型
@property (weak, nonatomic) IBOutlet UITextField *tf_time;//隐患评估时间
@property (weak, nonatomic) IBOutlet UITextField *tf_completionTime;//隐患整改时间
@property (weak, nonatomic) IBOutlet UITextField *tf_acceptanceTime;//隐患验收时间
@property (weak, nonatomic) IBOutlet UITextField *tf_money;
@property (weak, nonatomic) IBOutlet UITextField *tf_prevention;//防控措施
@property (weak, nonatomic) IBOutlet UITextField *tf_system;//制度措施清单
@property (weak, nonatomic) IBOutlet UITextField *tf_rectification;//整改措施
@property (weak, nonatomic) IBOutlet UITextView *tv_describe;//隐患描述

@property (weak, nonatomic) IBOutlet UIImageView *img_report;
@property (weak, nonatomic) IBOutlet UIImageView *img_reportDel;

@property (weak, nonatomic) IBOutlet UIImageView *img_reportTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_reportTwoDel;

@property (weak, nonatomic) IBOutlet UIImageView *img_reportThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_reportThreeDel;

@property (weak, nonatomic) IBOutlet UIImageView *img_rectification;
@property (weak, nonatomic) IBOutlet UIImageView *img_rectificationDel;

@property (weak, nonatomic) IBOutlet UIImageView *img_rectificationTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_rectificationTwoDel;

@property (weak, nonatomic) IBOutlet UIImageView *img_rectificationThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_rectificationThreeDel;

@property (weak, nonatomic) IBOutlet UITextView *tv_note;

/**隐患上报*/
@property (copy, nonatomic) NSString *hd_report_pic_one;
@property (copy, nonatomic) NSString *hd_report_pic_two;
@property (copy, nonatomic) NSString *hd_report_pic_three;
/**隐患整改*/
@property (copy, nonatomic) NSString *hd_rectification_pic_one;
@property (copy, nonatomic) NSString *hd_rectification_pic_two;
@property (copy, nonatomic) NSString *hd_rectification_pic_three;

/**
 隐患类别id(hd_category_id),
 隐患类别中文(hd_category_text),
 隐患等级id(hd_level_id),
 隐患等级中文(hd_level_text),
 隐患整改类型id(hd_rectification_type_id),
 隐患整改类型中文(hd_rectification_type_text),
 整改资金id(hd_rectification_funds_id),
 整改资金中文(hd_rectification_funds_text),
 */
@property (copy, nonatomic) NSString *hd_category_id;
@property (copy, nonatomic) NSString *hd_category_text;

@property (copy, nonatomic) NSString *hd_level_id;
@property (copy, nonatomic) NSString *hd_level_text;

@property (copy, nonatomic) NSString *hd_rectification_type_id;
@property (copy, nonatomic) NSString *hd_rectification_type_text;

@property (copy, nonatomic) NSString *hd_rectification_funds_id;
@property (copy, nonatomic) NSString *hd_rectification_funds_text;


@end

@implementation SQRegistrationViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"隐患登记";
    
    self.hd_report_pic_one = @"";
    self.hd_report_pic_two = @"";
    self.hd_report_pic_three = @"";
    self.hd_rectification_pic_one = @"";
    self.hd_rectification_pic_two = @"";
    self.hd_rectification_pic_three = @"";
   
    [SQGetDictionariesModel getDataWithParams:nil WithCallBack:^(SQGetDictionariesModel * data) {
        self.getDictionariesModel = data;
        }];
    
}

-(NSInteger)rowsCount:(NSInteger)section{
    return 0;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQConfirmTableViewCell"]];
}

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

#pragma mark -- 隐患类型
- (IBAction)clickType:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.getDictionariesModel == nil) {return;}
    
    self.getDictionariesModel.hd_categoryArr.pickerSigleShow(self.tf_type,@"隐患类别").finish = ^(UITextField * textField){
        SQGetDictionariesModelHd_category *categoryModel  = self.getDictionariesModel.hd_category[self.tf_type.selectIndex[0].intValue];
        self.hd_category_id = categoryModel.d_id;
        self.hd_category_text = categoryModel.d_name;
    };
    
}

#pragma mark -- 隐患等级
- (IBAction)clickLevel:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.getDictionariesModel == nil) {return;}
    
    self.getDictionariesModel.hd_levelArr.pickerSigleShow(self.tf_level,@"隐患等级").finish = ^(UITextField * textField){
        SQGetDictionariesModelHd_level *hd_level  = self.getDictionariesModel.hd_level[self.tf_level.selectIndex[0].intValue];
        self.hd_level_id = hd_level.d_id;
        self.hd_level_text = hd_level.d_name;
    };
    
}

#pragma mark --隐患整改类型
- (IBAction)clickRectification:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.getDictionariesModel == nil) {return;}
    
    self.getDictionariesModel.hd_rectification_typeArr.pickerSigleShow(self.tf_rectificationType,@"隐患整改类型").finish = ^(UITextField * textField){
     SQGetDictionariesModelHd_rectification_type *rectification_type  = self.getDictionariesModel.hd_rectification_type[self.tf_rectificationType.selectIndex[0].intValue];
        self.hd_rectification_type_id = rectification_type.d_id;
        self.hd_rectification_type_text = rectification_type.d_name;
    };
}

#pragma mark --隐患评估时间
- (IBAction)clickTime:(UIButton *)sender {
    [self.view endEditing:YES];
    HRBDateSelectView.showForType(HRBDateSelectTypeYMD).formatString(HRBTimeFormat_YMD).result = ^(NSString * _Nonnull time, NSDate * _Nonnull date) {
        
        self.tf_time.text = time;
    };
   
}

#pragma mark --计划完成时间
- (IBAction)clickCompletionTime:(UIButton *)sender {
    [self.view endEditing:YES];
    HRBDateSelectView.showForType(HRBDateSelectTypeYMD).formatString(HRBTimeFormat_YMD).result = ^(NSString * _Nonnull time, NSDate * _Nonnull date) {
        
        self.tf_completionTime.text = time;
    };
  
}

#pragma mark --隐患验收时间
- (IBAction)clickAcceptanceTime:(UIButton *)sender {
    [self.view endEditing:YES];
    HRBDateSelectView.showForType(HRBDateSelectTypeYMD).formatString(HRBTimeFormat_YMD).result = ^(NSString * _Nonnull time, NSDate * _Nonnull date) {
        
        self.tf_acceptanceTime.text = time;
    };
  
}

#pragma mark --整改资金
- (IBAction)clcikMoney:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.getDictionariesModel == nil) {return;}
    
    self.getDictionariesModel.hd_rectification_fundsArr.pickerSigleShow(self.tf_money,@"整改资金").finish = ^(UITextField * textField){
        SQGetDictionariesModelHd_rectification_funds *rectification_funds  = self.getDictionariesModel.hd_rectification_funds[self.tf_money.selectIndex[0].intValue];
        
        self.hd_rectification_funds_id = rectification_funds.d_id;
        self.hd_rectification_funds_text = rectification_funds.d_name;
       };
    
}

#pragma mark --隐患上报图1
- (IBAction)clcikReport:(UIButton *)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_pic_one]]) {
        [SQShowImage showImage:self.img_report];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_report.image = data[0];
            self.hd_report_pic_one = [UIImageJPEGRepresentation(self.img_report.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_reportDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
   
}



- (IBAction)clcikReportDel:(UIButton *)sender {
    self.img_reportDel.image = [UIImage imageNamed:@""];
    self.img_report.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_one = @"";
}


#pragma mark --隐患上报图2
- (IBAction)clcikReportTwo:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_two]]) {
        [SQShowImage showImage:self.img_reportTwo];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_reportTwo.image = data[0];
            self.hd_report_pic_two = [UIImageJPEGRepresentation(self.img_reportTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_reportTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
   

}


- (IBAction)clcikReportTwoDel:(UIButton *)sender {
    self.img_reportTwoDel.image = [UIImage imageNamed:@""];
    self.img_reportTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_two = @"";
}



#pragma mark --隐患上报图3
- (IBAction)clcikReportThree:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_three]]) {
        [SQShowImage showImage:self.img_reportThree];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_reportThree.image = data[0];
            self.hd_report_pic_three = [UIImageJPEGRepresentation(self.img_reportThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_reportThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
}


- (IBAction)clcikReportThreeDel:(UIButton *)sender {
    self.img_reportThreeDel.image = [UIImage imageNamed:@""];
    self.img_reportThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_three = @"";
}

#pragma mark --隐患整改图1
- (IBAction)clcikRectification:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_rectification_pic_one]]) {
        [SQShowImage showImage:self.img_rectification];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_rectification.image = data[0];
            self.hd_rectification_pic_one = [UIImageJPEGRepresentation(self.img_rectification.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_rectificationDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
}

-(IBAction)clcikRectificationDel:(UIButton *)sender {
    self.img_rectificationDel.image = [UIImage imageNamed:@""];
    self.img_rectification.image = [UIImage imageNamed:@"addImage"];
    self.hd_rectification_pic_one = @"";
}

#pragma mark --隐患整改图2
-(IBAction)clcikRectificationTwo:(UIButton *)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_rectification_pic_two]]) {
        [SQShowImage showImage:self.img_rectificationTwo];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_rectificationTwo.image = data[0];
            self.hd_rectification_pic_two = [UIImageJPEGRepresentation(self.img_rectificationTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_rectificationTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
   

}

-(IBAction)clcikRectificationTwoDel:(UIButton *)sender {
    self.img_rectificationTwoDel.image = [UIImage imageNamed:@""];
    self.img_rectificationTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_rectification_pic_two = @"";
}


#pragma mark --隐患整改图3
-(IBAction)clcikRectificationThree:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_rectification_pic_three]]) {
        [SQShowImage showImage:self.img_rectificationThree];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_rectificationThree.image = data[0];
            self.hd_rectification_pic_three = [UIImageJPEGRepresentation(self.img_rectificationThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_rectificationThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
   
}

-(IBAction)clcikRectificationThreeDel:(UIButton *)sender {
    self.img_rectificationThreeDel.image = [UIImage imageNamed:@""];
    self.img_rectificationThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_rectification_pic_three = @"";
}

#pragma mark --确认
- (IBAction)clickConfirm:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self verification]) {
  //整改资金
        /**方法用途：隐患登记。传参：隐患类别id(hd_category_id),隐患类别中文(hd_category_text),隐患等级id(hd_level_id),隐患等级中文(hd_level_text),隐患整改类型id(hd_rectification_type_id),隐患整改类型中文(hd_rectification_type_text),防控措施(hd_prevention_and_control_measures),整改措施(hd_rectification_measures),制度措施清单(hd_list_of_system_measures),隐患评估时间(hd_report_time),计划完成时间(hd_plan_complete),隐患验收时间(hd_acceptance_time),整改资金id(hd_rectification_funds_id),整改资金中文(hd_rectification_funds_text),企业id(hd_enterprise),上报人层级(m_level),上报人id(hd_report_person_id),上报人名字(hd_report_person_text),上报人部门id(hd_report_department_id),上报人部门中文(hd_report_department_text),隐患上报第一张照片(hd_report_pic_one),第二张照片(hd_report_pic_two),第三张照片(hd_report_pic_three),隐患整改第一张照片(hd_rectification_pic_one),第二张照片(hd_rectification_pic_two),第三张照片(hd_rectification_pic_three),隐患描述(hd_danger_description),台账备注(account_notes)。成功回参：code：200|隐患增加后返回的id。失败回参：code：0|msg。*/
        
        [SQDangeRegistrationModel getDataWithParams:@{@"hd_category_id":self.hd_category_id,
                                                      @"hd_category_text":self.hd_category_text,
                                                      @"hd_level_id":self.hd_level_id,
                                                      @"hd_level_text":self.hd_level_text,
                                                      @"hd_rectification_type_id":self.hd_rectification_type_id,
                                                      @"hd_rectification_type_text":self.hd_rectification_type_text,
                                                      @"hd_prevention_and_control_measures":self.tf_prevention.text,
                                                      @"hd_rectification_measures":self.tf_rectification.text,
                                                      @"hd_list_of_system_measures":self.tf_system.text,
                                                      @"hd_report_time":self.tf_time.text,
                                                      @"hd_plan_complete":self.tf_completionTime.text,
                                                      @"hd_acceptance_time":self.tf_acceptanceTime.text,
                                                      @"hd_rectification_funds_id":self.hd_rectification_funds_id,
                                                      @"hd_rectification_funds_text":self.hd_rectification_funds_text,
                                                      @"hd_enterprise":MEnterpriseId,
                                                      @"hd_enterprise_text":MEnterpriseText,
                                                      @"m_level":MLevel,
                                                      @"hd_report_person_id":UID,
                                                      @"hd_report_person_text":MName,
                                                      @"hd_report_department_id":MGroup,
                                                      @"hd_report_department_text":MNumber,
                                                      @"hd_report_pic_one":self.hd_report_pic_one?self.hd_report_pic_one:@"",
                                                      @"hd_report_pic_two":self.hd_report_pic_two?self.hd_report_pic_two:@"",
                                                      @"hd_report_pic_three":self.hd_report_pic_three?self.hd_report_pic_three:@"",
                                                      
                                                      @"hd_rectification_pic_one":self.hd_rectification_pic_one?self.hd_rectification_pic_one:@"",
                                                      @"hd_rectification_pic_two":self.hd_rectification_pic_two?self.hd_rectification_pic_two:@"",
                                                      @"hd_rectification_pic_three":self.hd_rectification_pic_three?self.hd_rectification_pic_three:@"",
                                                      @"hd_danger_description":self.tv_describe.text,
                                                      @"account_notes":self.tv_note.text?self.tv_note.text:@"",
    
        } WithCallBack:^(SQDangeRegistrationModel * data) {
            [CCToast showMessage:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
    
        }];
        
    }

   
    

    
}

#pragma mark ---- 验证
- (BOOL)verification {
    BOOL ret = NO;
    if([self.tf_type.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择隐患类型"];
        return NO;
    }
    
    
    if([self.tf_level.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择隐患等级"];
        return NO;
    }
    if([self.tf_rectificationType.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择隐患整改类型"];
        return NO;
    }
    if([self.tf_time.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择隐患评估时间"];
        return NO;
    }
    if([self.tf_completionTime.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择计划完成时间"];
        return NO;
    }
    
    if([self.tf_acceptanceTime.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择隐患验收时间"];
        return NO;
    }
    
    if([self.tf_money.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择整改资金"];
        return NO;
    }
    if([self.tf_prevention.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入防控措施"];
        return NO;
    }
    if([self.tf_system.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入制度措施清单"];
        return NO;
    }
    
    if([self.tf_rectification.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入整改措施"];
        return NO;
    }
    
    if([self.tv_describe.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入隐患描述"];
        return NO;
    }
    
    if (![self haveStr:@[self.hd_report_pic_one,self.hd_report_pic_two,self.hd_report_pic_three]]) {
        
        [CCToast showMessage:@"请上传隐患上报照片"];
        return NO;
        
    }
    if (![self haveStr:@[self.hd_rectification_pic_one,self.hd_rectification_pic_two,self.hd_rectification_pic_three]]) {
            
        [CCToast showMessage:@"请上传隐患整改照片"];
            return NO;
        
    }else{
        ret = YES;
    }
    return YES;
}

- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
